package com.typing.alarm.view;

import java.util.ArrayList;

import com.typing.alarm.R;
import com.typing.alarm.activity.AlarmDetailActivity;
import com.typing.alarm.database.DBManager;
import com.typing.alarm.value.TypingAlarmVO;

import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;



public class AlarmListView extends ListView implements OnItemClickListener{

	private Context mContext;
	private Activity mActivity;
	private ArrayList<TypingAlarmVO> list;
	
	private Builder dialog;
	private AlarmInfoAdapter adapter;

	private SQLiteDatabase m_db;
	private Cursor m_cursor;
	private DBManager dbMgr;
	private LinearLayout layout;
	public TextView vAlarmTime;
	public TextView vTypingText;
	public TextView vTypingSpeed;
	private LayoutInflater inflate;
	
	public AlarmListView(Context context) {
		super(context);
		mContext = context;
		mActivity = (Activity)context;
		inflate = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//		setBackgroundColor(Color.BLUE);
		list = getData();
		adapter =new AlarmInfoAdapter();
		setAdapter(adapter);
		setOnItemClickListener(this);
		
	}
	public void RefreshData(){
		
		if(adapter != null){
			list =getData();
			adapter.notifyDataSetChanged();
		}
	}
	private ArrayList<TypingAlarmVO> getData(){
			dbMgr = new DBManager(mContext);
			m_db = dbMgr.open();
	    	// rawQuery 함수를 이용한 데이터 질의
	    	return dbMgr.getAppInfo("SELECT * FROM "+DBManager.DATABASE_TABLE + " ORDER BY "+DBManager.KEY_ID+" ASC", null);
	}
	public class AlarmInfoAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			return list == null ? 0 : list.size();
		}

		@Override
		public Object getItem(int position) {
			return position;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
//			Log.d("", "getView===>>"+position);
			if(convertView ==null){
				layout  = (LinearLayout)inflate.inflate(R.layout.alarm_list_row, null);
			}else{
				layout =(LinearLayout)convertView;	
			}
			vAlarmTime = (TextView)layout.findViewById(R.id.set_alarm_time);
			vTypingText = (TextView)layout.findViewById(R.id.set_typing_text);
			vTypingSpeed = (TextView)layout.findViewById(R.id.set_typing_speed);
			
			TypingAlarmVO data = list.get(position);
			vAlarmTime.setText(data.getAlarmTime());
			vTypingText.setText(data.getTypingText());
			vTypingSpeed.setText(data.getTypingSpeed());
			return layout;
		}
		
	}
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
		Intent intent = new Intent(mContext, AlarmDetailActivity.class);
		intent.putExtra("alarm_data", list.get(position));
		mActivity.startActivity(intent);		
		
	}

	
}
